package com.dinetech.dao;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import com.dinetech.entidades.Actividades;
import com.dinetech.entidades.ResponsableActividad;
import com.dinetech.entidades.Session;


public class WSProgramacion {

	final String NAMESPACE = "http://www.CuyTechService.pe/WSservice2005/";//"http://192.168.1.10/WebService/";
	final String URL= "http://www.cuytechservice.pe/WsCuyTech2005/SoapDataServices.asmx";//"http://192.168.1.10/WSservice2005/SoapDataServices.asmx";
	String resultado;
	
	public ArrayList<Actividades> listarListViewActividades (int idpersonal, String fecha){
		
		ArrayList<Actividades> listaArray= new ArrayList<Actividades>();
	    		
		final String METHOD_NAME = "listar_horario";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("idpersonal", idpersonal);
		request.addProperty("fecha", fecha);							
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL, 6000);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				if(respSoap.getPropertyCount()>1){
					for(int i=0; i < respSoap.getPropertyCount(); i++){
						
						SoapObject ic =(SoapObject)respSoap.getProperty(i);
						ArrayList<String> lisdet = listarDetGalpon(Integer.parseInt( ic.getPropertyAsString("IdProgramacion")));
						String cadenaGalpones="";
						for(int e=0; e < lisdet.size(); e++){
							cadenaGalpones = cadenaGalpones + lisdet.get(e).toString() + "  ";
						}
	
						listaArray.add(new Actividades(Integer.parseInt( ic.getPropertyAsString("IdProgramacion")),
									ic.getPropertyAsString("NomActividad").toString(), ic.getPropertyAsString("Hora_Inicio"), 
									ic.getPropertyAsString("Hora_Fin"), ic.getPropertyAsString("Comentario"), 
									Integer.parseInt( ic.getPropertyAsString("IdProgramacionDetalle")), ic.getPropertyAsString("EstadoActividad"),
									ic.getPropertyAsString("CodTipoLugar"), cadenaGalpones,Integer.parseInt( ic.getPropertyAsString("TipoActividad")),
									ic.getPropertyAsString("CodActividad")));				
					}
				}else{
					listaArray.add(new Actividades(0, "No tiene actividades para hoy."));
				}
			
			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaArray ;
	}
		
	public ArrayList<ResponsableActividad> listar_Personal_Cbx (int idsucursal){
		
		ArrayList<ResponsableActividad> listaArray= new ArrayList<ResponsableActividad>();
	    		
		final String METHOD_NAME = "listar_Personal_Cbx";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("Idsucursal", idsucursal);							
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL, 5000);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				for(int i=0; i < respSoap.getPropertyCount(); i++){
					
					SoapObject ic =(SoapObject)respSoap.getProperty(i);
					listaArray.add(new ResponsableActividad(Integer.parseInt( ic.getPropertyAsString("IdPersonal")),ic.getPropertyAsString("Nombres").toString()));				
				}
			
			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaArray ;
	}
	
	public String actualizarActividad (int idProgDet, int Idpersonal, String Obs){
					    		
		final String METHOD_NAME = "actualizar_actividad";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		String Resultado;
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdProgDetalle", idProgDet);	
		request.addProperty("IdPersonal", Idpersonal);
		request.addProperty("Comentario", Obs);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
				
			HttpTransportSE transporte = new HttpTransportSE(URL, 5000);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);	
				SoapPrimitive resultado_xml = (SoapPrimitive)envelope.getResponse();
				Resultado= resultado_xml.toString();
				
				WSProgramacion wsP = new WSProgramacion();
				Session s = Session.getInstance();
				s.setLISTA_ACTIVIDADES(wsP.listarListViewActividades(Idpersonal, s.obtenerFechaActual()));
				
											
			}catch(Exception e){
				Resultado = e.getMessage().toString();
			}			
		
		return Resultado ;
	}
	
	public ArrayList<String> listarDetGalpon (int idProgramacion){
		
		ArrayList<String> listaArray= new ArrayList<String>();
	    	
		final String METHOD_NAME = "listarDetGalpon";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdProgramacion", idProgramacion);							
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				if(respSoap.getPropertyCount()>0)
				{
					for(int i=0; i < respSoap.getPropertyCount(); i++){
						
						SoapObject ic =(SoapObject)respSoap.getProperty(i);
						listaArray.add(ic.getPropertyAsString("NomUbicacion"));				
					}
				}
				else
				{
					listaArray.add("No se indico donde se realiza de la actividad");
				}
			
			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaArray ;
	}
	
	public ArrayList<String> listarDetPozasJaulas (int idProgramacion){
		
		ArrayList<String> listaArray= new ArrayList<String>();
	    		
		final String METHOD_NAME = "listarDetPozasJaulas";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("IdProgramacion", idProgramacion);							
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		
		
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);				
				SoapObject respSoap=(SoapObject)envelope.getResponse();
				
				if(respSoap.getPropertyCount()>0)
				{
					for(int i=0; i < respSoap.getPropertyCount(); i++){
						
						SoapObject ic =(SoapObject)respSoap.getProperty(i);
						listaArray.add(ic.getPropertyAsString("NomUbicacion"));				
					}
				}
				else
				{
					listaArray.add("No se indico donde se realiza de la actividad");
				}
				
			}catch(Exception e){
				resultado=e.getMessage().toString();
			}			
		
		return listaArray ;
	}

	public String mostrarRecomendacion (int TipoAct, String CodAct , int IdProgDet){
	    		
		final String METHOD_NAME = "mostrarRecomendacion";
		final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		String Resultado;
		
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    		
		request.addProperty("TipoActividad", TipoAct);	
		request.addProperty("CodActividad", CodAct);
		request.addProperty("IdProgramacionDetalle", IdProgDet );
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
				
			HttpTransportSE transporte = new HttpTransportSE(URL);
			
			
			try{
				transporte.call(SOAP_ACTION, envelope);	
				SoapPrimitive resultado_xml = (SoapPrimitive)envelope.getResponse();
								
				Resultado= resultado_xml.toString();
											
			}catch(Exception e){
				Resultado = e.getMessage().toString();
			}			
		
		return Resultado ;
	}
}
